// stores/counter.js
import { defineStore } from "pinia";

import {
  addressListRequest,
  addressAddRequest,
  addressRemoveRequest,
  addressUpdateRequest,
} from "@/libs/service/api/address/index";
import { ref } from "vue";
import { IAddressForm } from "../type/address";

import { ElNotification } from "element-plus";

/**
 * 根
 */
export const addressPinia = defineStore("address", () => {
  // state
  const list: any = ref<IAddressForm[]>([]);
  // actions
  async function listActions(playLoad: IAddressForm) {
    const result = await addressListRequest(playLoad);
    list.value = result.data;
  }

  async function addActions(playLoad: IAddressForm) {
    const result = await addressAddRequest(playLoad);
    if (result.code !== 200) {
      ElNotification({
        title: "失败",
        message: result.msg,
        type: "error",
      });
    }
    ElNotification({
      title: "成功",
      message: "提交成功",
      type: "success",
    });
    listActions({});
  }

  async function updateActions(playLoad: IAddressForm) {
    const result = await addressUpdateRequest(playLoad);
    if (result.code !== 200) {
      ElNotification({
        title: "失败",
        message: result.msg,
        type: "error",
      });
    }
    ElNotification({
      title: "成功",
      message: "提交成功",
      type: "success",
    });
    listActions({});
  }

  async function removeActions(playLoad: IAddressForm) {
    const result = await addressRemoveRequest(playLoad);
    if (result.code !== 200) {
      ElNotification({
        title: "失败",
        message: result.msg,
        type: "error",
      });
    }
    ElNotification({
      title: "成功",
      message: "删除成功",
      type: "success",
    });
    listActions({});
  }

  return {
    list,
    listActions,
    addActions,
    updateActions,
    removeActions,
  };
});
